﻿using ModuleHeal.Component;
using PluginAPI.Core.Attributes;
using PluginAPI.Events;
using Utils;



namespace ModuleHeal.Event
{
    public class HealEvent
    {
        [PluginEvent(PluginAPI.Enums.ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            if (ev.Player.Team != PlayerRoles.Team.SCPs)
            {
                TimeCoroutine.Delay(1f, () =>
                {
                    bool isGet = ev.Player.GameObject.TryGetComponent(out HealComponent output);
                    if (isGet)
                    {
                        UnityEngine.GameObject.Destroy(output);
                    }
                });

                return;
            }

            TimeCoroutine.Delay(1f, () =>
            {
                float maxHealth;

                switch (ev.Player.Role)
                {
                    case PlayerRoles.RoleTypeId.Scp173:
                        maxHealth = 
                        RoleCustom.Main.CodeLoad.Singleton.Config.SCP173Event.Health;
                        break;
                    case PlayerRoles.RoleTypeId.Scp106:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP106Event.Health;
                        break;
                    case PlayerRoles.RoleTypeId.Scp049:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP049Event.Health;
                        break;
                    case PlayerRoles.RoleTypeId.Scp096:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP096Event.Health;
                        break;
                    case PlayerRoles.RoleTypeId.Scp0492:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP0492Event.Health;
                        break;                 
                    case PlayerRoles.RoleTypeId.Scp939:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP939Event.Health;
                        break;                  
                    case PlayerRoles.RoleTypeId.Scp3114:
                        maxHealth = RoleCustom.Main.CodeLoad.Singleton.Config.SCP3114Event.Health;
                        break;
                    default:
                        maxHealth = 10;
                        break;
                }


                bool isGet = ev.Player.GameObject.TryGetComponent(out HealComponent output);
                if (!isGet)
                {
                    output = ev.Player.GameObject.AddComponent<HealComponent>();
                    output.Bind(ev.Player, maxHealth);
                }
            });
        }



        [PluginEvent(PluginAPI.Enums.ServerEventType.PlayerDeath)]
        public void OnPlayerDeath(PlayerDeathEvent ev)
        {
            TimeCoroutine.Delay(1f, () =>
            {
                bool isGet = ev.Player.GameObject.TryGetComponent(out HealComponent output);
                if (isGet)
                {
                    UnityEngine.GameObject.Destroy(output);
                }
            });
        }

        [PluginEvent(PluginAPI.Enums.ServerEventType.PlayerLeft)]
        public void OnPlayerLeft(PlayerLeftEvent ev)
        {
            TimeCoroutine.Delay(0.1f, () =>
            {
                if (ev.Player == null) return;

                bool isGet = ev.Player.GameObject.TryGetComponent(out HealComponent output);
                if (isGet)
                {
                    UnityEngine.GameObject.Destroy(output);
                }
            });
        }
    }
}
